﻿using Public.Logger;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace ChongJu.CameraCalibration.Calibration
{
    public class CalibrationClient
    {
        private IntPtr _ptr;

        /// <summary>
        /// 创建对象
        /// </summary>
        public void Create()
        {
            if (_ptr == IntPtr.Zero)
                _ptr = CalibrationSDK.CreateJY_CC();
        }

        /// <summary>
        /// 获取版本号
        /// </summary>
        /// <param name="version"></param>
        /// <returns></returns>
        public string GetVersion()
        {
            try
            {
                return null;

                StringBuilder sb = new StringBuilder();
                CalibrationSDK.GetVersion(_ptr, sb);
                return sb.ToString();
            }
            catch(Exception ex)
            {
                Logger.Error("获取校准dll版本异常", ex);
                return null;
            }
        }

        /// <summary>
        /// 释放类
        /// </summary>
        public void DisposeJY_CC()
        {
            if (_ptr == null)
                return;

            CalibrationSDK.DisposeJY_CC(_ptr);
        }

        /// <summary>
        /// 设置自动校准选择ROI区域
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="w"></param>
        /// <param name="h"></param>
        /// <returns></returns>
        public bool SetRol(int x, int y, int w, int h)
        {
            return CalibrationSDK.Set_ROI(_ptr, x, y, w, h);
        }

        public bool FreeImage(IntPtr img)
        {
            return CalibrationSDK.FreeImage(img);
        }


        public bool GetImageRGB(IntPtr image, int x, int y, int w, int h,
            ref double r_mean,
            ref double g_mean,
            ref double b_mean,
            ref double r_st,
            ref double g_st,
            ref double b_st)
        {
            return CalibrationSDK.Get_image_ROI_RGB_mean(
                _ptr, image, x, y, w, h,
                ref r_mean,
                ref g_mean,
                ref b_mean,
                ref r_st,
                ref g_st,
                ref b_st);
        }

        /// <summary>
        /// 设置校准系数，参数依次为类，rgb三通道目标校系数
        /// </summary>
        /// <param name="r_factor1"></param>
        /// <param name="g_factor1"></param>
        /// <param name="b_factor1"></param>
        /// <returns></returns>
        public bool SetAdjustmentFactor(double r_factor1, double g_factor1, double b_factor1)
        {
            return CalibrationSDK.Set_adjustment_factor(_ptr, r_factor1, g_factor1, b_factor1);
        }

        /// <summary>
        /// 设置目标校准值，参数依次为类，rgb三通道目标校准值
        /// </summary>
        /// <param name="r_target1"></param>
        /// <param name="g_target1"></param>
        /// <param name="b_target1"></param>
        /// <returns></returns>
        public bool SetAdjustmentTarget(double r_target1, double g_target1, double b_target1)
        {
            return CalibrationSDK.Set_adjustment_target(_ptr, r_target1, g_target1, b_target1);
        }

        /// <summary>
        /// 获取校准建议，参数依次为类，
        /// rgb三通道均值，校准成功阈值，
        /// RGB三通道增益变化的大小，校准完成标志（0：参数校准，1：微调，2：调整完成）；
        /// </summary>
        /// <param name="r_mean"></param>
        /// <param name="g_mean"></param>
        /// <param name="b_mean"></param>
        /// <param name="the"></param>
        /// <param name="r_gain_amount"></param>
        /// <param name="g_gain_amount"></param>
        /// <param name="b_gain_amount"></param>
        /// <param name="flag_Iteration"></param>
        /// <returns></returns>
        public bool GetPropose(double r_mean, double g_mean, double b_mean, double the,
            ref int r_gain_amount, ref int g_gain_amount, ref int b_gain_amount, ref int flag_Iteration)
        {
            return CalibrationSDK.Get_propose(_ptr, r_mean, g_mean, b_mean, the,
                ref r_gain_amount, ref g_gain_amount, ref b_gain_amount, ref flag_Iteration);
        }

        /// <summary>
        /// 获取去头去尾的图像均值和方差
        /// </summary>
        /// <param name="dirPath"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="w"></param>
        /// <param name="h"></param>
        /// <param name="max_num"></param>
        /// <param name="min_p"></param>
        /// <param name="max_p"></param>
        /// <param name="flag"></param>
        /// <param name="r_mean"></param>
        /// <param name="g_mean"></param>
        /// <param name="b_mean"></param>
        /// <param name="r_std"></param>
        /// <param name="g_std"></param>
        /// <param name="b_std"></param>
        /// <returns></returns>
        public bool Get_dir_ROI_RGB_mean(
            IntPtr dirPath, int x, int y, int w, int h, 
            int max_num, 
            double min_p, double max_p, 
            double flag, 
            ref double r_mean, ref double g_mean, ref double b_mean, 
            ref double r_std, ref double g_std, ref double b_std)
        {
            return CalibrationSDK.Get_dir_ROI_RGB_mean(_ptr, dirPath, x, y, w, h, max_num, min_p, max_p, flag,
                ref r_mean, ref g_mean, ref b_mean,
                ref r_std, ref g_std, ref b_std);
        }

        public bool Get_brightness_propose(
            double r_mean, double g_mean, double b_mean, 
            ref double gain_amount, 
            ref int flag_Iteration)
        {
            return CalibrationSDK.Get_brightness_propose(_ptr, r_mean, g_mean, b_mean, ref gain_amount, ref flag_Iteration);
        }

        public IntPtr Add_center_mark(int _width, int _height, int channel, IntPtr image, int step,
            int center_x, int center_y, ref int newWidth, ref int newHeight, ref int newStep, int cir_size)
        {
            return CalibrationSDK.Add_center_mark(_ptr, _width, _height, channel, image, step, center_x, center_y,
                ref newWidth, ref newHeight, ref newStep, cir_size);
        }

        public bool SaveStandHuibanImg(IntPtr dirPath, int x, int y, int w, int h, int max_num, double min_p, double flag)
        {
            return CalibrationSDK.SaveStandHuibanImg(_ptr, dirPath, x, y, w, h, max_num, min_p, flag);
        }
    }
}
